package com.lifeonwalden.game.mindustry.processor;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.lifeonwalden.game.mindustry.Global;
import com.lifeonwalden.game.mindustry.entity.Bullet;
import com.lifeonwalden.game.mindustry.feature.Control;
import com.lifeonwalden.game.mindustry.world.Coordinate;

import java.util.Optional;

public class KeyPressedProcessor {
    public Control control;

    public KeyPressedProcessor(Control control) {
        this.control = control;
    }

    public void process() {
        short xMove = 0, yMove = 0;
        if (Gdx.input.isKeyPressed(Input.Keys.A)) {
            xMove--;
        }
        if (Gdx.input.isKeyPressed(Input.Keys.D)) {
            xMove++;
        }
        if (Gdx.input.isKeyPressed(Input.Keys.W)) {
            yMove++;
        }
        if (Gdx.input.isKeyPressed(Input.Keys.S)) {
            yMove--;
        }
        if (!(0 == xMove && 0 == yMove)) {
            control.world().moveTo(control.world().player, xMove, yMove);
        }

        if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)) {
            if (null == Global.buildOperation) {
                Coordinate targetPoint = Global.world.projectedCoordinate(Gdx.input.getX(), Gdx.input.getY());
                Optional<Bullet> bullet = Global.world.player.shoot(targetPoint.x, targetPoint.y);
                if (bullet.isPresent()) {
                    Global.world.addBullet(bullet.get());
                }
            }
        }
    }
}
